/* 路由 vue-router */

import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

// 组件
import Home from '../view/home/index.vue'
const HomeHot = r => require.ensure([], () => r(require('../view/home/home_hot.vue')), 'home')
const HomeFollow = r => require.ensure([], () => r(require('../view/home/home_follow.vue')), 'home')

import Dynamic from '../view/dynamic/index.vue'

import Sign from '../view/sign/index.vue'

import User from '../view/user/index.vue'

let _home = [
  {
    path: 'homeHot',
    name: 'homeHot',
    component: HomeHot,
    meta: {title: '热门'}
  },
  {
    path: 'homeFollow',
    name: 'homeFollow',
    component: HomeFollow,
    meta: {title: '关注'}
  }
]


const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/index',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: Home,
      meta: {title: '首页'},
      redirect: '/home/homeHot',
      children: [
        ..._home
      ]
    },
    {
      path: '/dynamic',
      name: 'dynamic',
      component: Dynamic,
      meta: {title: '动态', keepAlive: true}
    },
    {
      path: '/sign',
      name: 'sign',
      component: Sign,
      meta: {title: '签到'}
    },
    {
      path: '/user',
      name: 'user',
      component: User,
      meta: {title: '用户'}
    }
  ]
})

router.beforeEach((to, from, next) => {
  // console.log(to);
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router
